package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.crawler.CrawlHTML;
import com.kdtech.crawler.at.UrlArgumentTop;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.RegexUtils;
import com.kdtech.utils.StringUtils;
import com.kdtech.utils.HtmlCleaner;

/**
 *	未完成
 * http://www.cfi.net.cn/p20110808000575.html  中国财经信息网
 *
 * @author KK
 *
 */
public class CfiNewsAnalyse implements AnalyseNews {

	private static final String[] regex={
		"http://.*.cfi.cn/p[0-9]+.html",
		"http://www.cfi.cn/p[0-9]+.html",
		"http://www.cfi.net.cn/p[0-9]+.html",
		"http://www.cfi.cn/newspage.aspx\\?id=[0-9]*[&p=[0-9]]*",
		"http://www.cfi.net.cn/newspage.aspx\\?id=[0-9]*[&p=[0-9]]*",


	};

	
	public boolean isDetailPage(String url) {
		return RegexUtils.matchAnyIgnoreCase(url, regex);
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		if (urlMeta.getHtml() == null) {
		}

		NewsMeta news=new NewsMeta();
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();
		if(!isDetailPage(url)){
		}
		String title=null;
		String content=null;
		Long date=null;
		Document doc=Jsoup.parse(htmltxt);
		news.setUrl(url);

		title =doc.select("h1").text();
		if(StringUtils.isBlank(title)){
			title =doc.select("title").text();
			if(title.contains("-")) title=StringUtils.substringBefore(title, "-");
		}
		date=DateUtils.matchDate(doc.select("td[width=40%]").text());
		if(date==null){
			date=DateUtils.matchDate(url);
		}
		doc.select("td[align=right],#pagebottom,td[style=font-size:10.8pt;width:13%],h1,td[width=40%]").remove();
		content=HtmlCleaner.getContentHtml(url ,doc.select("div#tdcontent"));
		news.setTitle(StringUtils.trimSpace(title));
		news.setContent(content);
		news.setDate(date);
		news.setType(1);
		return news;
	}
	public static void main(String[] args) {
		String url="http://economy.cfi.cn/p20161107000194.html";
		url=UrlArgumentTop.FromatUrl(url);
		System.out.println(url);
		CfiNewsAnalyse analyse=new CfiNewsAnalyse();
		System.out.println(analyse.isDetailPage(url));
		UrlMeta meta=CrawlHTML.responseToURL(url);
		System.out.println(analyse.parserHtml(meta));
	}

	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}

	
	public boolean isNeedUpdate(){
		return false;
	}
}
